package com.example.teaculture.tools;

import android.util.Log;

import com.example.teaculture.model.Headline;
import com.example.teaculture.model.TeaBusiness;
import com.example.teaculture.model.TeaInfo;
import com.example.teaculture.model.Teaknown;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JsoupUtils {


    public static List<TeaInfo> getTeaInfo(String baseUrl, String html) {
        List<TeaInfo> list = new ArrayList<>();
        new Thread(() -> {
            try {
                Document doc = Jsoup.connect(baseUrl + html).get();
                Elements elements = doc.select("dl.school-list-cnt");
                for (Element element : elements) {
                    String title = element.select("dd.school-cnt").select("a.school-tit").select("b").text();
                    String info = element.select("dd.school-cnt").select("p.clearfix.mt").text();
                    String image = element.select("dt.school-logo").select("a").select("img").attr("src");
                    String url = element.select("dt.school-logo").select("a").attr("href");
                    String dates[] = element.select("dd.school-cnt").select("p.clearfix").select("span.sp-xuezhi").text().split(" : ");
                    TeaInfo teaInfo = new TeaInfo(title, info, baseUrl + url, dates[1], baseUrl + image);
                    list.add(teaInfo);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        return list;
    }

    //getTeaknown("https://chaaowang.com/fenlei/changshi", "/page/2") 从第二页开始
    public static List<Teaknown> getTeaknown(String baseUrl, String html) throws InterruptedException {
        List<Teaknown> list = new ArrayList<>();
        new Thread(() -> {
            try {
                Document doc = Jsoup.connect(baseUrl + html).get();
                Elements elements = doc.select("ul.post-loop.post-loop-default.cols-0");
                elements = elements.select("li.item");
                System.out.println(elements);
                for (Element element : elements) {
                    String title = element.select("div.item-content").select("h2.item-title").select("a").text();
                    String date = element.select("div.item-content").select("div.item-meta").select("span.item-meta-li.date").text();
                    String newDate = date.replace("年", "-").replace("月", "-").replace("日", "");
                    String info = element.select("div.item-content").select("div.item-excerpt").text();
                    String image = element.select("div.item-img").select("a.item-img-inner").select("img").attr("src");
                    String url = element.select("div.item-img").select("a.item-img-inner").attr("href");
                    list.add(new Teaknown(title, url, info, newDate, image));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        return list;
    }

    //getHeadline("http://cha.quanxi.cc/toutiao/TypesList/7030", "/1") 从第一页开始
    public static List<Headline> getHeadline(String baseUrl, String html) throws InterruptedException {
        List<Headline> list = new ArrayList<>();
        new Thread(() -> {
            try {
                Document doc = Jsoup.connect(baseUrl + html).get();
                Elements elements = doc.select("div.left");
                elements = elements.select("a.news-item-txtImg");
                for (Element element : elements) {
                    String image = element.select("div.txtImg-img").select("el-image").attr("src");
                    String info = element.select("div.txtImg-txt").select("div.des").text();
                    String title = element.select("div.txtImg-img").select("el-image").attr("alt");
                    Element element1 = element.select("div.txtImg-txt").select("div.foot").get(0);
                    String date = element1.child(2).text();
                    String url = element.attr("href");
                    url = "http://cha.quanxi.cc/" + url;
                    list.add(new Headline(title, url, info, date, image));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        return list;
    }

    //getTeaEncyclopedia("https://chaaowang.com/fenlei/changshi","/page/1");
    public static List<TeaInfo> getTeaEncyclopedia(String baseUrl, String html) {
        List<TeaInfo> list = new ArrayList<>();
        new Thread(() -> {
            try {
                Document doc = Jsoup.connect(baseUrl + html).get();
                Elements elements = doc.select("li.item");
                for (Element element : elements) {
                    String title = element.select("div.item-img").select("a.item-img-inner").attr("title");
                    String info = element.select("div.item-content").select("div.item-excerpt").select("p").text();
                    String image = element.select("img.j-lazy").attr("data-original");
                    String url = element.select("div.item-img").select("a.item-img-inner").attr("href");
                    String date = element.select("div.item-content").select("div.item-meta").select("span.item-meta-li.date").text();
                    String newDate = date.replace("年", "-").replace("月", "-").replace("日", "");
                    TeaInfo teaInfo = new TeaInfo(title, info, url, newDate, image);
                    list.add(teaInfo);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        return list;
    }

    //    private static final String BASE_URL = "http://www.zgchawang.com/news/list/1044/";
    //    html = "/2/";
    public static List<TeaInfo> getTeaBusiness(int page) {
        String baseUrl = "http://zhannei.baidu.com/cse/search?q=%E8%8C%B6%E7%BB%8F%E8%90%A5&p=" + page + "&s=2942127813356630428&nsid=&entry=1";
        List<TeaInfo> list = new ArrayList<>();
        new Thread(() -> {
            try {
                Document doc = Jsoup.connect(baseUrl).get();
                Elements elements = doc.select("div.result");
                for (Element element : elements) {
                    String title = element.select("h3.c-title").select("a").text();
                    String url = element.select("h3.c-title").select("a").attr("href");
                    String image = element.select("td").select("img").attr("src");
                    String info = element.select("div.c-abstract").text();
                    String date = element.select("div").select("span.c-showurl").text().split(" ")[1];
                    TeaInfo teaBusiness = new TeaInfo(title, info, url, date, image);
                    list.add(teaBusiness);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        return list;
    }
}
